// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();

// 云函数入口函数
//查询帖子 
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  // var {num,page} = event;
  var num = event.num
  var page = event.page
  var dataResult =  await db.collection("trival").orderBy("posttime","desc") .skip(num).limit(page).get()
  // .where({delstate:true})
  var dataArr = dataResult.data
  if(dataArr.length>0)
  {
    for(var i=0;i < dataArr.length;i++)
    {

        var count = await db.collection("xiangcun_zan").where({
          openid:openid,
          commonid:dataArr[i]._id
        }).count();
        if(count.total)
        {
          dataArr[i].isZan = false
        }
        else{
          dataArr[i].isZan = true
        }


        var count2 = await db.collection("xiangcun_zan").where({
          commonid:dataArr[i]._id
        }).count();
        dataArr[i].zanSize = count2.total;

        var commNum = await db.collection("xiangcun_comment").where({
          commentid:dataArr[i]._id
        }).count();
        dataArr[i].commNum = commNum.total;

        if(openid==dataArr[i].openid){
          dataArr[i].delState=true
        }else{
          dataArr[i].delState=false
        }
        
    }
  }
  return dataArr;
}